﻿/*
supplyRange.js
zhouh@zd_20160423 
*/
$(function () {
    //announcement
    var uid = $('[id$="hdfuid"]').val();
    var loadPercent = 0; //20 20 20 20 20
    var savePercent = 0; //10 15 15 20 20 20 
    var productionID = "";
    //function
    function loadProduction() {//加载产品
        $.get('../SellManage/SupplyRange.aspx', {
            handler: 'getProductList',
            u: uid,
            r: Math.random()
        }, function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                $("#divProducts").empty();
                for (var i = 0; i < o.length; i++) {
                    var obj = document.createElement("div");
                    $(obj).addClass("Pros");
                    if (o[i]["isused"] != "") {
                        $(obj).addClass("proSelected");
                    }
                    $(obj).attr("args", o[i]["ProductionID"]);
                    $(obj).html(o[i]["desc"]);
                    $("#divProducts").append($(obj));
                }
            }
            $('.Pros').button().click(function (elt) {
                $("#ActContent").animate({ "left": "-100%" }, "normal", function () {

                    var target = elt.currentTarget
                    selectProduction($(target).attr("args"));
                    $('#Pros').html(" —— " + $(target).html());
                    $("#step1").removeClass("selected");
                    $("#step2").addClass("selected");
                })
            });
        })
    }
    function SaveProduction() {
        $.post('../SellManage/SupplyRange.aspx', {
            handler: 'setProduction',
            pid: productionID,
            uid: uid,
            r: Math.random()
        }, function (data) {
            if (data == "OK") {
                savePercent += 10;
            }
            else {
                savePercent += 10;
            }
        })
    }
    function returnToChoosePro() {
        if (confirm("返回将导致您之前的操作丢失，您确定吗？")) {
            $("#ActContent").animate({ "left": "0" }, "normal", function () {
                location.reload();
            })
        }
    }
    function selectProduction(PID) { //选择产品
        //开始加载参数
        productionID = PID;
        checkLoading();
        loadAll();
    }
    function loadMaterial(callback) {
        $.get("../SellManage/SupplyRange.aspx", {
            handler: "getMaterialList",
            pid: productionID,
            r: Math.random()
        }, function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                var content = $('#divMID_B').children('.itemContent').eq(0);
                var item = content.html();
                content.empty();
                for (var i = 0; i < o.length; i++) {
                    var code_d = o[i]['Material_code']; //锻件
                    var code_z = o[i]['Material_code_z']; //铸件
                    var mid = o[i]['MID']; //材质编号
                    var desc = code_z == code_d ? code_d : code_d + '/' + code_z;
                    var _item = item.replace('材料ID', mid).replace('材料名称', desc);
                    content.html(content.html() + _item);
                }
            }
            loadPercent += 20;
            if (callback) {
                callback();
            }
        })
    }
    function setMaterial() {
        $.get('../SellManage/SupplyRange.aspx', {
            handler: 'getMaterialUsed',
            uid: uid,
            pid: productionID,
            r: Math.random()
        },
        function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                for (var i = 0; i < o.length; i++) {
                    var mid = o[i]['MID'];
                    $('#divMID_B').children('.itemContent').children('.ckbItem').each(function (index, elt) {
                        var materialID = $(elt).children(':input').attr('args');
                        if (mid == materialID) {
                            $(elt).children(':input').prop('checked', 'true').change();
                        }
                    })
                }
            }
        })
    }
    function SaveMaterial() {
        var list = "";
        $('#divMID_B').children('.itemContent').children('.ckbItem').each(function (index, elt) {
            var materialID = $(elt).children(':input').attr('args');
            if ($(elt).children(':input').prop('checked')) {
                list += (materialID + "|");
            }
        })
        if (list != "") {
            list = list.substr(0, list.length - 1);
            $.post('../SellManage/SupplyRange.aspx', {
                handler: 'setMaterial',
                list: list,
                pid: productionID,
                uid: uid
            }, function (data) {
                if (data == "OK") {
                    savePercent += 15;
                }
            })
        }
        else {
            savePercent += 15;
        }
    }
    function loadSize(callback) {
        $.get("../SellManage/SupplyRange.aspx", {
            handler: "getSizeList",
            pid: productionID,
            r: Math.random()
        }, function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                var content = $('#divSize').children('.itemContent').eq(0);
                var item = content.html();
                content.empty();
                for (var i = 0; i < o.length; i++) {
                    var code_c = o[i]['Size_Code_C']; //公制
                    var code_s = o[i]['Size_Code_S']; //英制
                    var sid = o[i]['SID']; //尺寸编号
                    var desc = "(公)" + code_c + " (英)" + code_s;
                    var _item = item.replace('尺寸ID', sid).replace('尺寸名称', desc);
                    content.html(content.html() + _item);
                }
            }
            loadPercent += 20;
            if (callback) {
                callback();
            }
        })
    }
    function setSize() {
        $.get('../SellManage/SupplyRange.aspx', {
            handler: 'getSizeUsed',
            uid: uid,
            pid: productionID,
            r: Math.random()
        },
        function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                for (var i = 0; i < o.length; i++) {
                    var sid = o[i]['SID'];
                    $('#divSize').children('.itemContent').children('.ckbItem').each(function (index, elt) {
                        var SizeID = $(elt).children(':input').attr('args');
                        if (SizeID == sid) {
                            $(elt).children(':input').prop('checked', 'true').change();
                        }
                    })
                }
            }
        })
    }
    function SaveSize() {
        var list = "";
        $('#divSize').children('.itemContent').children('.ckbItem').each(function (index, elt) {
            var SID = $(elt).children(':input').attr('args');
            if ($(elt).children(':input').prop('checked') != "") {
                list += (SID + "|");
            }
        })
        if (list != "") {
            list = list.substr(0, list.length - 1);
            $.post('../SellManage/SupplyRange.aspx', {
                handler: 'setSize',
                list: list,
                uid: uid,
                pid: productionID
            }, function (data) {
                if (data == "OK") {
                    savePercent += 15;
                }
            })
        }
        else {
            savePercent += 15;
        }
    }
    function loadPressure(callback) {
        $.get("../SellManage/SupplyRange.aspx", {
            handler: "getPressureList",
            pid: productionID,
            r: Math.random()
        }, function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                var content = $('#divPressure').children('.itemContent').eq(0);
                var item = content.html();
                content.empty();
                for (var i = 0; i < o.length; i++) {
                    var code_c = o[i]['Pressure_Code_C']; //公制
                    var code_s = o[i]['Pressure_Code_S']; //英制
                    var pid = o[i]['PressureID']; //尺寸编号
                    var desc = "(公)" + code_c + " (英)" + code_s;
                    var _item = item.replace('压力ID', pid).replace('压力等级名称', desc);
                    content.html(content.html() + _item);
                }
            }
            loadPercent += 20;
            if (callback) {
                callback();
            }
        })
    }
    function setPressure() {
        $.get('../SellManage/SupplyRange.aspx', {
            handler: 'getPressureUsed',
            uid: uid,
            pid: productionID,
            r: Math.random()
        },
        function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                for (var i = 0; i < o.length; i++) {
                    var pid = o[i]['PressureID'];
                    $('#divPressure').children('.itemContent').children('.ckbItem').each(function (index, elt) {
                        var PressureID = $(elt).children(':input').attr('args');
                        if (PressureID == pid) {
                            $(elt).children(':input').prop('checked', 'true').change();
                        }
                    })
                }
            }
        })
    }
    function SavePressure() {
        var list = "";
        $('#divPressure').children('.itemContent').children('.ckbItem').each(function (index, elt) {
            var PressureID = $(elt).children(':input').attr('args');
            if ($(elt).children(':input').prop('checked') != "") {
                list += (PressureID + "|");
            }
        })
        if (list != "") {
            list = list.substr(0, list.length - 1);
            $.post('../SellManage/SupplyRange.aspx', {
                handler: 'setPressure',
                list: list,
                uid: uid,
                pid: productionID
            }, function (data) {
                if (data == "OK") {
                    savePercent += 20;
                }
            })
        }
        else {
            savePercent += 20;
        }
    }
    function loadSeal(callback) {
        $.get("../SellManage/SupplyRange.aspx", {
            handler: "getSealList",
            pid: productionID,
            r: Math.random()
        }, function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                var content = $('#divSeal').children('.itemContent').eq(0);
                var item = content.html();
                content.empty();
                for (var i = 0; i < o.length; i++) {
                    var SID = o[i]["SealLevelID"];
                    var LevelType = o[i]["LevelType"];
                    var Level = o[i]["Level"];
                    var desc = LevelType + " " + Level;
                    var _item = item.replace("密封标准名称", desc).replace("密封标准ID", SID);
                    content.html(content.html() + _item);
                }
            }
            loadPercent += 20;
            if (callback) {
                callback();
            }
        })
    }
    function setSeal() {
        $.get('../SellManage/SupplyRange.aspx', {
            handler: 'getSealUsed',
            uid: uid,
            pid: productionID,
            r: Math.random()
        },
        function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                for (var i = 0; i < o.length; i++) {
                    var sid = o[i]['SealID'];
                    $('#divSeal').children('.itemContent').children('.ckbItem').each(function (index, elt) {
                        var SealID = $(elt).children(':input').attr('args');
                        if (sid == SealID) {
                            $(elt).children(':input').prop('checked', 'true').change();
                        }
                    })
                }
            }
        })
    }
    function SaveSeal() {
        var list = "";
        $('#divSeal').children('.itemContent').children('.ckbItem').each(function (index, elt) {
            var SealID = $(elt).children(':input').attr('args');
            if ($(elt).children(':input').prop('checked') != "") {
                list += (SealID + "|");
            }
        })
        if (list != "") {
            list = list.substr(0, list.length - 1);
            $.post('../SellManage/SupplyRange.aspx', {
                handler: 'setSeal',
                list: list,
                uid: uid,
                pid: productionID
            }, function (data) {
                if (data == "OK") {
                    savePercent += 20;
                }
            })
        }
        else {
            savePercent += 20;
        }
    }
    function loadTemperature(callback) {
        $.get("../SellManage/SupplyRange.aspx", {
            handler: "getTempList",
            r: Math.random()
        }, function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                var content = $('#divTemp').children('.itemContent').eq(0);
                var item = content.html();
                content.empty();
                for (var i = 0; i < o.length; i++) {
                    var TempID = o[i]["TempID"];
                    var up = o[i]["UP"];
                    var down = o[i]["Down"];
                    var desc = o[i]["TDesc"] + "(" + down + "-" + up + ")";
                    var _item = item.replace("温度标准名称", desc).replace("温度标准ID", TempID);
                    content.html(content.html() + _item);
                }
            }
            loadPercent += 20;
            if (callback) {
                callback();
            }
        })
    }
    function setTemperature() {
        $.get('../SellManage/SupplyRange.aspx', {
            handler: 'getTempUsed',
            uid: uid,
            pid: productionID,
            r: Math.random()
        },
        function (data) {
            var o = JSON.parse(data);
            if (o != null && o.length > 0) {
                for (var i = 0; i < o.length; i++) {
                    var tid = o[i]['TempID'];
                    $('#divTemp').children('.itemContent').children('.ckbItem').each(function (index, elt) {
                        var TempID = $(elt).children(':input').attr('args');
                        if (tid == TempID) {
                            $(elt).children(':input').prop('checked', 'true').change();
                        }
                    })
                }
            }
        })
    }
    function SaveTemprature() {
        var list = "";
        $('#divTemp').children('.itemContent').children('.ckbItem').each(function (index, elt) {
            var TempID = $(elt).children(':input').attr('args');
            if ($(elt).children(':input').prop('checked') != "") {
                list += (TempID + "|");
            }
        })
        if (list != "") {
            list = list.substr(0, list.length - 1);
            $.post('../SellManage/SupplyRange.aspx', {
                handler: 'setTemp',
                list: list,
                uid: uid,
                pid: productionID
            }, function (data) {
                if (data == "OK") {
                    savePercent += 20;
                }
            })
        }
        else {
            savePercent += 20;
        }
    }
    function checkControl(top, className) { //checkbox控制
        $(top).change(function () {
            $('.' + className).each(function (index, elt) {
                $(elt).prop('checked', $(top).prop('checked'));
            })
        })
    }
    function lblCkb() { //关联文本
        $('.lblCkb').each(function (index, elt) {
            $(elt).click(function () {
                var status = $(elt).prev(':input').prop('checked');
                if (status || status == 'checked') {
                    $(elt).prev(':input').prop('checked', '').change();
                }
                else {
                    $(elt).prev(':input').prop('checked', 'checked').change();
                }
            })
        })
    }
    function getCounts() { //获取已选数量，处理变更
        cal();
        $('[type="checkbox"]').change(function () {
            cal();
        })
        function cal() {
            $('.itemContent').each(function (index, elt) {
                var label = $(elt).parent('div').prev().find('.num');
                var count = 0;
                $(elt).children('.ckbItem').each(function (index2, elt2) {
                    if ($(elt2).find(':input').prop('checked')) {
                        count += 1;
                    }
                })
                label.html(count);
            })
        }
    }
    function loadAll() {
        $("#dialog").dialog("open");
        loadMaterial(function () {//加载材质
            setMaterial(); //设置材质
        })
        loadSize(function () {//加载口径
            setSize();
        })
        loadPressure(function () {//加载压力等级
            setPressure();
        })
        loadSeal(function () {//加载密封等级
            setSeal();
        })
        loadTemperature(function () {//加载温度
            setTemperature();
        })
    }
    function checkLoading() {//检查加载
        var t = setTimeout(function () {
            if (loadPercent != 100) {
                checkLoading();
                $('#progressbar').progressbar({ value: loadPercent });
            }
            else {
                lblCkb();
                getCounts();
                $("#dialog").dialog("close");
                clearTimeout(t);
            }
        }, 500);
    }
    function checkSaving() {//检查保存
        var t = setTimeout(function () {
            if (savePercent != 100) {
                $('#progressbar').progressbar({ value: savePercent });
                checkSaving();
            }
            else {
                $("#dialog").dialog("close");
                clearTimeout(t);
                $("#ActContent").animate({ "left": "0" }, "normal", function () {
                    location.reload();
                })
            }
        }, 500)
    }
    function SaveAll() {
        SaveProduction();
        SaveMaterial(); //保存材质
        SaveSize();
        SavePressure();
        SaveSeal();
        SaveTemprature();
    }
    function delProduction() {
        if (confirm("该操作会删除该产品及其所有参数，且无法撤销，您确定要这么做吗？")) {
            $.get("../SellManage/SupplyRange.aspx", {
                handler: "delete",
                pid: productionID,
                uid: uid
            }, function (data) {
                if (data == "OK") {
                    $("#ActContent").animate({ "left": "0" }, "normal", function () {
                        location.reload();
                    })
                }
                else {
                    console.log(data);
                }
            })
        }
    }
    $('#accordingContent').accordion({//初始化UI
        collapsible: true,
        heightStyle: 'content'
    });
    $("#dialog").dialog({//初始化Dialog
        autoOpen: false,
        width: 400,
        buttons: [
		{
		    text: "Cancel",
		    click: function () {
		        $(this).dialog("close");
		    }
		}]
    });
    //loadAll();
    //checkLoading();
    loadProduction();
    $('#step1').click(function () {
        returnToChoosePro();
    })

    //handlers
    checkControl($('#ckbMID_B_All'), "ckbMID_B_Sub");
    checkControl($('#ckbSize_All'), "ckbSize_Sub");
    checkControl($('#ckbPressure_All'), "ckbPressure_Sub");
    checkControl($('#ckbSeal_All'), "ckbSeal_Sub");
    checkControl($('#ckbTemp_All'), "ckbTemp_Sub");
    //init

    $('#button').button().click(function () {
        $("#dialog").dialog("open");
        $('#progressbar').progressbar({ value: 0 }); //初始化进度条
        SaveAll();
        checkSaving();
    })
    $('#btnDel').button().click(function () {
        delProduction();
    })
})